import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {useLoginStore} from "@/stores";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'login',
            component: () => import('@/views/LoginView.vue'),
        },
        {
            path: '/regist',
            name: 'regist',
            component: () => import('@/views/RegistView.vue'),
        },
        {
            path: '/home',
            name: 'home',
            component: HomeView,
            meta: {role: ['admin', 'user', 'manager']},
            children: [
                {
                    path: 'basic',
                    name: 'basic',
                    component: () => import('@/views/home/BasicView.vue'),
                    meta: {
                        role: ['admin', 'user', 'manager'],
                        title: "首页",
                        is_menu: true,
                        icon: "House"
                    },
                },
                {
                    path: 'user',
                    name: 'user',
                    component: () => import('@/views/home/UserView.vue'),
                    meta: {role: ['admin'], title: "用户管理", is_menu: true, icon: "User",},
                },
                {
                    path: 'drug',
                    name: 'drug',
                    component: () => import('@/views/home/DrugView.vue'),
                    meta: {role: ['admin', 'user', 'manager'], title: "药品信息管理", is_menu: true, icon: "Notebook",},
                },
                {
                    path: 'buy',
                    name: 'buy',
                    component: () => import('@/views/home/BuyView.vue'),
                    meta: {role: ['admin', 'user', 'manager'], title: "进货管理", is_menu: true, icon: "Sell",},
                },
                {
                    path: 'sell',
                    name: 'sell',
                    component: () => import('@/views/home/SellingView.vue'),
                    meta: {role: ['admin', 'user', 'manager'], title: "销售管理", is_menu: true, icon: "SoldOut",},
                },
                {
                    path: 'stock',
                    name: 'stock',
                    component: () => import('@/views/home/StockView.vue'),
                    meta: {role: ['admin', 'manager'], title: "药品库存管理", is_menu: true, icon: "DataBoard",},
                },
                {
                    path: 'client',
                    name: 'client',
                    component: () => import('@/views/home/ClientView.vue'),
                    meta: {role: ['admin', 'user', 'manager'], title: "客户管理", is_menu: true, icon: "Reading",},
                },
                {
                    path: 'supply',
                    name: 'supply',
                    component: () => import('@/views/home/SupplierView.vue'),
                    meta: {role: ['admin', 'manager'], title: "供应商管理", is_menu: true, icon: "TakeawayBox",},
                },
                {
                    path: 'solve',
                    name: 'solve',
                    component: () => import('@/views/home/SolveView.vue'),
                    meta: {role: ['admin'], title: "劣化药品处理", is_menu: true, icon: "DeleteFilled",},
                },
            ]
        }
    ]
})

// router.beforeEach((to, from, next) => {
//     if (to.meta.role) {
//         const loginStore = useLoginStore();
//         const user_role = loginStore.role
//         const allowed_roles = Array.isArray(to.meta.role) ? to.meta.role : [to.meta.role]; // 确保它是一个数组
//
//         if (allowed_roles.indexOf(user_role) === -1) {
//             next({name: 'login'})
//         } else {
//             next()
//         }
//     } else {
//         next()
//     }
// })

export default router
